<template>
  <EcDialog v-model="dialogVisible" :auto-height="true" class="form-dialog" top="10vh" width="55%">
    <!-- 标题 -->
    <template #title>
      <div class="dialog-title">
        <SvgIcon :icon="dialogIconMap[dialogMode]"/>
        <span>{{ dialogModeMap[dialogMode] + ' - ' + dialogTitle }}</span>
      </div>
    </template>

    <!-- 表单数据 -->
    <el-form ref="formRef"
             v-loading="dataLoading"
             :inline="true"
             :model="formData"
             :rules="formRules"
             :disabled="readonly"
             label-width="auto"
             status-icon>

      <el-form-item label="配置名称：" prop="name" style="width:98%;max-width: unset">
        <el-input v-model="formData.name" maxlength="128" placeholder="请输入配置名称" show-word-limit/>
      </el-form-item>

      <el-form-item label="产品售价：" prop="price">
        <el-input-number v-model="formData.price" :min="0.01" :precision="2" :step="1" controls-position="right" placeholder="请输入产品售价"/>
      </el-form-item>

      <el-form-item label="默认拿货价：" prop="price_cost_def">
        <el-input-number v-model="formData.price_cost_def" :min="0" :precision="2" :step="1" controls-position="right" placeholder="请输入拿货价"/>
      </el-form-item>

      <el-form-item label="公司拿货价：" prop="price_cost_corp">
        <el-input-number v-model="formData.price_cost_corp" :min="0" :precision="2" :step="1" controls-position="right" placeholder="请输入拿货价"/>
      </el-form-item>

      <el-divider border-style="dashed" content-position="left">级别拿货价</el-divider>

      <el-form-item label="普通拿货价：" prop="price_cost_level0">
        <el-input-number v-model="formData.price_cost_level0" :min="0" :precision="2" :step="1" controls-position="right" placeholder="请输入拿货价"/>
      </el-form-item>

      <el-form-item label="达人拿货价：" prop="price_cost_level1">
        <el-input-number v-model="formData.price_cost_level1" :min="0" :precision="2" :step="1" controls-position="right" placeholder="请输入拿货价"/>
      </el-form-item>

      <el-form-item label="V1拿货价：" prop="price_cost_level2">
        <el-input-number v-model="formData.price_cost_level2" :min="0" :precision="2" :step="1" controls-position="right" placeholder="请输入拿货价"/>
      </el-form-item>

      <el-form-item label="V2拿货价：" prop="price_cost_level3">
        <el-input-number v-model="formData.price_cost_level3" :min="0" :precision="2" :step="1" controls-position="right" placeholder="请输入拿货价"/>
      </el-form-item>

      <el-form-item label="V3拿货价：" prop="price_cost_level4">
        <el-input-number v-model="formData.price_cost_level4" :min="0" :precision="2" :step="1" controls-position="right" placeholder="请输入拿货价"/>
      </el-form-item>

      <el-divider border-style="dashed" content-position="left">V3平级奖励配置</el-divider>

      <el-form-item label="平级奖励1：" prop="diff_reward1">
        <el-input-number v-model="formData.diff_reward1" :min="0" :precision="2" :step="1" controls-position="right" placeholder="请输入拿货价"/>
      </el-form-item>
      <el-form-item label="平级奖励2：" prop="diff_reward2">
        <el-input-number v-model="formData.diff_reward2" :min="0" :precision="2" :step="1" controls-position="right" placeholder="请输入拿货价"/>
      </el-form-item>
      <el-form-item label="平级奖励3：" prop="diff_reward3">
        <el-input-number v-model="formData.diff_reward3" :min="0" :precision="2" :step="1" controls-position="right" placeholder="请输入拿货价"/>
      </el-form-item>
    </el-form>

    <!-- 底部操作区域 -->
    <template #footer>
      <span class="dialog-footer">
        <el-button :loading="dataLoading" @click="dialogVisible = false">取 消</el-button>
        <el-button v-if="!readonly" v-permission="['config:private_price_cost:create', 'config:private_price_cost:update']" :loading="dataLoading" type="primary" @click="onSubmitForm()">确 定</el-button>
      </span>
    </template>
  </EcDialog>
</template>

<script lang="ts" setup>
import { computed, reactive, ref } from 'vue';
import { ElLoading, ElMessage, ElNotification, FormInstance, FormRules } from 'element-plus';
import { EcDialog } from '@/components/dialog';
import { SvgIcon } from '@/components/icon';
import useDialog from '@/hooks/use-dialog.ts';
import { PrivatePriceCostConfig, privatePriceCostConfigInfo, privatePriceCostConfigSaveOrUpdate } from '@/api/api-config.ts';

defineOptions({name: 'private-price-cost-config-form'})

const formRef = ref<FormInstance>();
const formLoading = ref(false); // 表单是否加载中
const {dialogVisible, dialogTitle, dialogMode, dialogModeMap, dialogIconMap} = useDialog(false);

/** 默认表单数据 */
const defaultForm: PrivatePriceCostConfig = <PrivatePriceCostConfig>{id: 0};

// 表单数据
const formData = ref({...defaultForm});

// 表单验证规则
const formRules = reactive<FormRules>({
  name: [{required: true, message: '名称不能为空', trigger: 'blur'}],
  price: [{required: true, message: '产品售价不能为空', trigger: 'blur'}],
  price_cost_def: [{required: true, message: '默认拿货价不能为空', trigger: 'blur'}],
  price_cost_corp: [{required: true, message: '公司拿货价不能为空', trigger: 'blur'}],
  price_cost_level0: [{required: true, message: '普通拿货价不能为空', trigger: 'blur'}],
  price_cost_level1: [{required: true, message: '达人拿货价不能为空', trigger: 'blur'}],
  price_cost_level2: [{required: true, message: 'V1拿货价不能为空', trigger: 'blur'}],
  price_cost_level3: [{required: true, message: 'V2拿货价不能为空', trigger: 'blur'}],
  price_cost_level4: [{required: true, message: 'V3拿货价不能为空', trigger: 'blur'}],
  diff_reward1: [{required: true, message: 'V3平级奖励1不能为空', trigger: 'blur'}],
  diff_reward2: [{required: true, message: 'V3平级奖励2不能为空', trigger: 'blur'}],
  diff_reward3: [{required: true, message: 'V3平级奖励3不能为空', trigger: 'blur'}],
});

/** 判断数据是否处于加载中 */
const dataLoading = computed(() => {
  return formLoading.value;
})

/** 是否只读 */
const readonly = computed(() => {
  return dialogMode.value == 0;
});

/** 是否新增 */
const isCreate = computed(() => {
  return dialogMode.value == 1 || formData.value.id <= 0;
});

/** 打开弹窗 */
const open = async (mode: number, id?: number) => {
  dialogMode.value = mode;
  dialogTitle.value = '私域拿货价配置';
  dialogVisible.value = true;
  // 重置表单
  handleFormReset();

  // 加载数据
  if (id) {
    formLoading.value = true
    try {
      // 加载用户数据
      const {data} = await privatePriceCostConfigInfo(id)
      formData.value = data;
    } finally {
      formLoading.value = false
    }
  }
}
// 提供 open 方法，用于打开弹窗
defineExpose({open});

/** 表单提交 */
const emit = defineEmits(['refresh']);
const onSubmitForm = async () => {
  await formRef.value?.validate((valid, fields) => {
    if (!valid) {
      ElMessage({type: 'error', message: '请检查是否有必填项未完善',});
      return
    }
    const loading = ElLoading.service({
      lock: true,
      text: '正在提交中...',
      background: 'rgba(0, 0, 0, 0.2)'
    })
    // 提交数据
    privatePriceCostConfigSaveOrUpdate(formData.value, isCreate.value).then(response => {
      // 提示信息
      ElNotification({title: '成功', message: '操作完成', type: 'success', duration: 2000})
      // 发送操作成功的事件
      emit('refresh')
      // 隐藏窗口
      dialogVisible.value = false
    }).finally(() => loading.close())

  });
};

/** 重置表单 */
const handleFormReset = () => {
  // 重置表单数据
  formData.value = {...defaultForm};
  // 重置界面表单数据
  formRef.value?.resetFields();
};

</script>

<style lang="scss" scoped>
.form-dialog {
  .el-input-number {
    width: 180px;
  }

  .el-form--inline .el-form-item {
    width: 350px;
  }
}
</style>
